package chapter2;

/**
 * @author Hang
 * @date 2022/8/12
 * @project AutumnOffer
 * @description 面试题15
 **/
public class hammingWeight {
    public static void main(String[] args) {
        System.out.println(hammingWeight(15));
    }
    public static int hammingWeight(int n){
        int res = 0;
        while(n!=0){ // 不等于0就消除最右边的1
            n = n & (n-1); //消除操作
            res++; //加加
        }
        return res;
    }
}
